WEBAPP['pageChat'] = function() {
	bum.chat.init();
};

bum.chat = {
	url : bum.socket.pollUrl + '/chat',
	state : 0,

	inited : false,
	init : function() {
		if (!bum.socket.inited) {
			console.log('init socket...');
			bum.socket.init();
		}
		
		if (bum.chat.inited) {
			return;
		}
		
		// đăng ký lắng nghe data về app chat
		bum.socket.app[bum.chat.keys.APP_CHAT] = bum.chat.onData;		

		// lắng nghe kết nối socket ready để bắt đầu kết nối chat
		if (bum.socket.ready) {
			bum.chat.connect();
		} else {
			bum.listener.add(bum.socket.SOCKET_READY, function() {
				console.log('connect...');
				bum.chat.connect();
			});
		}

		// khởi tạo gui
		bum.chat.gui.init();
		
		bum.chat.inited = true;
	},

	onData : function(message) {
		switch (message.func) {
		case bum.chat.keys.FUNC_CONNECT:
			bum.chat.onConnected(message.data);
			break;

		case bum.chat.keys.FUNC_PAIR:
			bum.chat.onPaired(message.data);
			break;

		case bum.chat.keys.FUNC_SEND_MSG:
			bum.chat.onMsg(message.data);
			break;

		case bum.chat.keys.FUNC_UNPAIR:
			bum.chat.onUnpaired(message.data);
			break;
		}
	},

	// kết nối vào chat
	connected : false,
	connect : function() {
		if (bum.chat.state !== bum.chat.keys.STATE_DISCONNECT) {
			return;
		}

		bum.chat.state = bum.chat.keys.STATE_CONNECTING;

		$.post(bum.chat.url, {
			act : bum.chat.keys.FUNC_CONNECT,
			id : bum.socket.id,
			wsc : bum.socket.wsc
		});
	},
	onConnected : function() {
		bum.chat.connected = true;
		bum.chat.state = bum.chat.keys.STATE_CONNECTED;

		bum.chat.gui.onConnected();
	},
	disconnect : function() {
		bum.chat.state = bum.chat.keys.STATE_DISCONNECT;

		$.post(bum.chat.url, {
			act : bum.chat.keys.FUNC_DISCONNECT,
			id : bum.socket.id,
			wsc : bum.socket.wsc
		});
	},

	pair : function(data) {
		bum.chat.state = bum.chat.keys.STATE_PAIRING;

		$.post(bum.chat.url, {
			act : bum.chat.keys.FUNC_PAIR,
			id : bum.socket.id,
			wsc : bum.socket.wsc
		});

		bum.chat.gui.onPairing();
	},
	onPaired : function(data) {
		bum.chat.state = bum.chat.keys.STATE_PAIRED;

		bum.chat.gui.onPaired(data);
	},

	sendMsg : function(text) {
		$.post(bum.chat.url, {
			act : bum.chat.keys.FUNC_SEND_MSG,
			id : bum.socket.id,
			wsc : bum.socket.wsc,
			text : text
		});
	},
	onMsg : function(data) {
		bum.chat.gui.onMsg(data);
	},

	unpair : function() {
		bum.chat.state = bum.chat.keys.STATE_UNPAIRING;
		$.post(bum.chat.url, {
			act : bum.chat.keys.FUNC_UNPAIR,
			id : bum.socket.id,
			wsc : bum.socket.wsc
		});
	},
	onUnpaired : function(data) {
		bum.chat.state = bum.chat.keys.STATE_CONNECTED;

		bum.chat.gui.onUnpaired();
	}

};

bum.chat.gui = {
	init : function() {
		$('#chatForm').submit(function() {
			if (bum.chat.state !== bum.chat.keys.STATE_PAIRED) {
				if (bum.chat.state === bum.chat.keys.STATE_CONNECTED) {
					$('#chatStartStop').click();
				}
				return false;
			}

			var T = $(this).find('.chat-msg-text');
			var text = $.trim(T.val());
			T.val('').focus();

			if (text) {
				bum.chat.gui.addMyMsg(text);
				bum.chat.sendMsg(text);
			}

			return false;
		}).on('click touch', '.start-stop-btn', function() {
			var T = $(this);

			switch (bum.chat.state) {
			case bum.chat.keys.STATE_CONNECTED:
			case bum.chat.keys.STATE_PAIRING:
				bum.chat.pair();
				break;

			case bum.chat.keys.STATE_PAIRED:
				if (confirm('Bye người này?')) {
					bum.chat.unpair();
				}
				break;
			}

			// clear
			T = null;
		});

		// $(window).resize(function() {
		// $('#chatMsgs').height($(window).height() - 95);
		// }).resize();

		bum.chat.gui.msgsContainer = $('#chatMsgs');

		// template
		var T = $('#chatMsgsTemplate');
		bum.chat.gui.template.helpConnect = T.find('.help-connect .list-group-item');
		bum.chat.gui.template.helpPairing = T.find('.help-paring .list-group-item');
		bum.chat.gui.template.helpStart = T.find('.help-start .list-group-item');
		bum.chat.gui.template.helpExit = T.find('.help-exit .list-group-item');
		bum.chat.gui.template.msg = T.find('.msg .list-group-item');
		bum.chat.gui.template.msgMy = T.find('.my-msg .list-group-item');
	},

	msgsContainer : null,
	template : {
		helpConnect : null,
		helpPairing : null,
		helpStart : null,
		helpExit : null,
		msg : null,
		msgMy : null
	},

	onConnected : function() {
		bum.chat.gui.msgsContainer.empty().append(bum.chat.gui.template.helpConnect.clone());
	},
	onPairing : function() {
		bum.chat.gui.msgsContainer.empty().append(bum.chat.gui.template.helpPairing.clone());
	},
	onPaired : function(data) {
		bum.chat.gui.msgsContainer.empty().append(bum.chat.gui.template.helpStart.clone());

		bum.chat.gui.addMsg(data.message);
		$('#chat-msg').focus();

		$('#chatStartStop').removeClass('btn-info').addClass('btn-warning').text('Exit');
	},

	onMsg : function(data) {
		bum.chat.gui.addMsg(data.message);
	},
	addMyMsg : function(text) {
		var msg = bum.chat.gui.template.msgMy.clone().text(text);
		bum.chat.gui.msgsContainer.append(msg);

		bum.chat.gui.scroll(msg);

		// clear
		msg = null;
	},
	addMsg : function(text) {
		var msg = bum.chat.gui.template.msg.clone().find('span').text(text).end();
		bum.chat.gui.msgsContainer.append(msg);
		bum.chat.gui.scroll(msg);

		// clear
		msg = null;
	},
	scroll : function(msg) {
		var top = msg.offset().top;
		$('html, body').animate({
			scrollTop : top
		}, 'normal');
	},

	onUnpaired : function() {
		bum.chat.gui.msgsContainer.append(bum.chat.gui.template.helpExit.clone());
		$('#chatStartStop').removeClass('btn-warning').addClass('btn-info').text('Start');
	}
};

bum.chat.keys = {
	APP_CHAT : 1,

	FUNC_CONNECT : 1,
	FUNC_PAIR : 2,
	FUNC_UNPAIR : 3,
	FUNC_SEND_MSG : 4,
	FUNC_PING : 5,
	FUNC_DISCONNECT : 6,

	STATE_DISCONNECT : 0,
	STATE_CONNECTING : 1,
	STATE_CONNECTED : 2,
	STATE_PAIRING : 3,
	STATE_PAIRED : 4,
	STATE_UNPAIRING : 5,
	STATE_UNPAIRED : 2,
};

WEBAPP['pageChatUnload'] = function() {
	bum.chat.disconnect();
	bum.socket.stop();
};